﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using ThreeByte.ActiveDeck.Config;
using System.Collections.ObjectModel;

namespace ThreeByte.ActiveDeck.Converters
{
    public class TextToCategoryWordsConverter : IValueConverter
    {
        //ObservableCollection<Word> --> string
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            ObservableCollection<Word> collection = value as ObservableCollection<Word>;
            if(collection == null) {
                return null;
            }

            StringBuilder sb = new StringBuilder();
            foreach(Word w in collection) {
                sb.AppendLine(w.Text);
            }
            return sb.ToString();
        }

        //string --> ObservableCollection<Word>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            string wordString = value as string;
            if(wordString == null) {
                return null;
            }

            ObservableCollection<Word> collection = new ObservableCollection<Word>();
            string[] words = wordString.Split('\n', '\r');
            foreach(string w in words) {
                if(!(string.IsNullOrWhiteSpace(w))) {
                    collection.Add(new Word(w));
                }
            }

            return collection;
        }
    }
   
}
